package xyz.xtt.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import xyz.xtt.admin.constants.AssetHandleEnum;
import xyz.xtt.admin.constants.AssetTypeEnum;
import xyz.xtt.admin.request.AssetRequest;
import xyz.xtt.admin.request.GiftRequest;
import xyz.xtt.admin.service.IAssetService;
import xyz.xtt.admin.vo.AssetHandleVo;
import xyz.xtt.admin.vo.AssetListVo;
import xyz.xtt.admin.vo.AssetVo;
import xyz.xtt.asset.client.request.MintRequest;
import xyz.xtt.asset.client.response.MintResponse;
import xyz.xtt.common.response.ApiResult;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 资产基础信息表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/asset")
public class AssetController {
    @Autowired
    private IAssetService assetService;


    @GetMapping("/list")
    public ApiResult<Page<AssetListVo>> list(AssetRequest assetRequest) throws Exception {
        Integer current = Optional.ofNullable(assetRequest.getCurrent()).filter(x -> x > 1).orElse(1);
        Integer pageSize = Optional.ofNullable(assetRequest.getPageSize()).filter(x -> x > 1).orElse(20);
        assetRequest.setCurrent(current);
        assetRequest.setPageSize(pageSize);
        return ApiResult.ok(assetService.getList(assetRequest));
    }

    @GetMapping("/detail")
    public ApiResult<AssetVo> get(@Validated @NotNull(message = "ID不能为空") String id) {
        Long assetId = Long.parseLong(id);
        return ApiResult.ok(assetService.get(assetId));
    }

    @GetMapping("/class_list")
    public ApiResult<Map<Integer, String>> getAssetClassList() {
        Map<Integer, String> map = AssetTypeEnum.toMap();
        return ApiResult.ok(map);
    }

    @PostMapping("/mint")
    public ApiResult<MintResponse> mint(@RequestBody @Valid MintRequest mintRequest) {
        MintResponse result = assetService.mint(mintRequest);
        return Objects.nonNull(result) ? ApiResult.ok(result) : ApiResult.failure();
    }

    @PostMapping("/transferFrom")
    public ApiResult<?> transferFrom(@RequestBody @Valid GiftRequest giftRequest) {
        Boolean result = assetService.transferFrom(giftRequest);
        return ApiResult.ok(result);
    }

    @GetMapping("/set_asset_sell")
    public ApiResult<Boolean> seAssetSellPattern(@Validated @NotBlank(message = "资产ID不能为空") String id,
                                                 @Validated @NotBlank(message = "GoodsId不能为空") String goodsId,
                                                 @Validated @NotNull(message = "状态不能为空") Integer status) {
        Long idL = Long.valueOf(id);
        return ApiResult.ok(assetService.seAssetSellPattern(idL, goodsId, status));
    }

    /**
     * 此接口目前提供两个功能：空投资产给用户、回收用户资产到原始账户
     */
    @PostMapping("/handle")
    public ApiResult<Object> assetHandle(@RequestPart @Valid AssetHandleVo handleVo,
                                    @RequestParam MultipartFile file,
                                    @RequestHeader("uid") String uid,
                                    HttpServletRequest req) throws Exception {
        log.info("requestUrl:{},requestParams:{}", req.getRequestURI(), handleVo.toString());
        String[] uids = {"1", "2"};
        boolean isAdmin = Arrays.asList(uids).contains(uid);
        boolean check = AssetHandleEnum.checkMethod(handleVo.getMethod());
        if (!isAdmin) {
            return ApiResult.build(1, "Maye be you are not admin", null);
        } else if (!check) {
            return ApiResult.build(1, "{method} of params is error", null);
        }
        Integer count;
        switch (handleVo.getMethod()) {
            case "drop" -> count = assetService.assetDrop(handleVo, file);
            case "reuse" -> count = assetService.assetReuse(handleVo, file);
            default -> count = 0;
        }
        return ApiResult.ok(count);

    }

}
